package com.asimes.flink;

import org.apache.commons.io.FileUtils;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.configuration.Configuration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class TestDistributedCache {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();
        environment.registerCachedFile("D:\\Download\\distributedcache.txt", "distributedCache");
        DataSource<String> dataSource = environment.fromElements("Linea", "Lineb", "Linec", "Lined");
        DataSet<String> result = dataSource.map(new RichMapFunction<String, String>() {
            private static final long serialVersionUID = 3130834804747943010L;
            private List<String> dataList = new ArrayList<>();

            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                File myFile = getRuntimeContext().getDistributedCache().getFile("distributedCache");
                List<String> lines = FileUtils.readLines(myFile);
                for (String line : lines) {
                    dataList.add(line);
                    System.out.println("分布式缓存为：" + line);
                }
            }

            @Override
            public String map(String value) throws Exception {
                System.out.println("使用datalist：" + dataList + "-------" + value);
                return dataList + ": " + value;
            }
        }).setParallelism(2);

        result.printToErr();
    }
}
